import cherrypy

from MusicModule import MusicModule
from NovelModule import NovelModule
from TranslateModule import TranslateModule
from NetworkModule import NetworkModule
from BaikeModule import BaikeModule


class Root(object):

    def __init__(self):
        self.novel_module = NovelModule()

    @cherrypy.expose
    def novel_main(self):
        return """<!DOCTYPE html>
<html>
<head>
    <meta charset=\"UTF-8\">
    <title>Roxy Novel Module</title>
    <style>
        .inp{
            padding:5px;
            font-size:24px;
            border-radius:10px;
            width:500px;
            height:45px;
        }
        .btn{
            background-color:green;
            color:white;
            font-size:30px;
            border-radius:10px;
            border:none;
            width:120px;
            height:60px;
        }
    </style>
</head>
<body>
<center><br><br><br><h1>Roxy Novel - Search</h1><br><br>
<form action="http://localhost:8009/search_novel/" method="get">
    <input type="text" class="inp" name="key" placeholder="请输入书名或作者...">
    <input type="submit" class="btn" value="搜索">
</form>
</center>
</body>
</html>"""

    @cherrypy.expose
    def search_novel(self, key):
        self.novel_reslist = self.novel_module.search_novel(key)
        # for i in self.novel_reslist:
        #     self.res.append(str("\\" + i["name"]))
        # self.res_num = len(self.res)
        content =  "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Roxy Novel Module</title><style>body {margin: 0;padding: 100px 100px 100px 100px;}h2 {font-size: 30px;}a {font-size: 24px; font-color:black;}</style></head><body><h2>SEARCH RESULT:</h2>"
        for i in self.novel_reslist:
            content += f"<a href=\"http://localhost:8009/get_chapter/?url={i['url']}\">{i['name']}</a><br>"
        content += "</body></html>"
        return content

    @cherrypy.expose
    def get_chapter(self, url):
        self.chapter_reslist = self.novel_module.get_chapter(url)
        content = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Roxy Novel Module</title><style>body {margin: 0;padding: 100px 100px 100px 100px;}h2 {font-size: 30px;}a {font-size: 24px; font-color:black;}</style></head><body>"
        for i in self.chapter_reslist:
            content += f"<a href=\"http://localhost:8009/read/?url={i['url']}\">{i['name']}</a><br>"
        content += "</body></html>"
        return content

    @cherrypy.expose
    def read(self, url):
        novel_module = NovelModule()
        res = novel_module.get_content(url)
        content = """<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Roxy Novel Module</title><style>body {margin: 0;padding: 100px 100px 100px 100px;}h1 {font-size: 40px;text-align:center;}p {font-size: 24px;}</style></head><body>"""
        # content += "<h1>" + title + "</h1><br>"
        # print (self.now_content)
        for i in res:
            content += "<p>" + i + "</p>"
        content += "</body></html>"
        return content

    @cherrypy.expose
    def baike(self, key):
        baike_module = BaikeModule()
        url = "https://baike.baidu.com/search/word?fromModule=lemma_search-box&lemmaId=lemma_search-box&word=" + key
        res = baike_module.get_request(url)
        content = """<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Roxy Novel Module</title><style>body {margin: 0;padding: 100px 100px 100px 100px;}h1 {font-size: 40px;}p {font-size: 24px;}h1 {font-size:45px;} h2 {font-size:30}</style></head><body>"""
        content += "<h1>" + res["main_title"] + "</h1>"
        content += "<h2>" + res["sub_title"] + "</h2><br><br>"
        content += "<p>" + res["text"] + "</p>"
        content += "</body></html>"
        return content


class RoxyTools:

    def __init__(self):
        self.novel_module = NovelModule()

    def run_cherrypy(self):
        print("Running network at localhost:8009")
        cherrypy.config.update({'server.socket_port': 8009})
        cherrypy.quickstart(Root(), '/')


if __name__ == "__main__":
    roxy_tools = RoxyTools()
    roxy_tools.run_cherrypy()